//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "FormItemDrugGenericNames.h"
#include "FormItemDrugPharmGroupsSets.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "DBGridEh"
#pragma link "DBGridEhGrouping"
#pragma link "FIBDatabase"
#pragma link "FIBDataSet"
#pragma link "GridsEh"
#pragma link "pFIBDatabase"
#pragma link "pFIBDataSet"
#pragma link "PropFilerEh"
#pragma link "PropStorageEh"
#pragma link "TemplateListEditor"
#pragma link "FIBQuery"
#pragma link "pFIBQuery"
#pragma resource "*.dfm"
TItemDrugGenericNamesForm *ItemDrugGenericNamesForm;
//---------------------------------------------------------------------------
__fastcall TItemDrugGenericNamesForm::TItemDrugGenericNamesForm(TComponent* Owner)
	: TListEditorTemplate(Owner)
{
}
//---------------------------------------------------------------------------

void __fastcall TItemDrugGenericNamesForm::FormShow(TObject *Sender)
{
	if(DataSet->Active || PharmGroupsSetDataSet->Active) throw Exception("Datasets already active");
	DataSet->Open();
	PharmGroupsSetDataSet->Open();
	PharmGroupsSetDataSet->First();
    DBGridEh->SetFocus();
}
//---------------------------------------------------------------------------

void __fastcall TItemDrugGenericNamesForm::FormCloseQuery(TObject *Sender, bool &CanClose)
{
	if(Transaction->Active && (Transaction->HasUncommitedUpdates || DataSet->State == dsInsert || DataSet->State == dsEdit || IsPharmGroupsSetChanged())) {
		int result = MessageDlg("Save changes ?", mtConfirmation, TMsgDlgButtons()<<mbYes<<mbNo<<mbCancel, NULL, mbYes);
		switch(result) {
			case mrYes:
				if(DataSet->State == dsInsert || DataSet->State == dsEdit) DataSet->Post();
				if(IsPharmGroupsSetChanged()) SavePharmGroupsSet();
				Transaction->Commit();
				break;
			case mrNo:
				Transaction->Rollback();
				break;
			case mrCancel:
				CanClose = false;
				break;
		}
	}
//
}
//---------------------------------------------------------------------------

void __fastcall TItemDrugGenericNamesForm::FormKeyDown(TObject *Sender, WORD &Key,
		  TShiftState Shift)
{
	if(Key == VK_ESCAPE && Shift == TShiftState() && (DataSet->State == dsInsert || DataSet->State == dsEdit)) {
		DataSet->Cancel();
		DBGridEh->SetFocus();
	}
	else if(Key == VK_RETURN && Shift == TShiftState())
		DBGridEh->SetFocus();
	else TListEditorTemplate::FormKeyDown(Sender, Key, Shift);

}
//---------------------------------------------------------------------------

bool __fastcall TItemDrugGenericNamesForm::IsPharmGroupsSetChanged()
{
	PharmGroupsSetDataSource->Enabled = false;
	int pharm_group_id = PharmGroupsSetDataSet->FBN("ID")->AsInteger;
	PharmGroupsSetDataSet->First();
	while(!PharmGroupsSetDataSet->Eof) {
		if(PharmGroupsSetDataSet->CachedUpdateStatus() == cusModified) {
			PharmGroupsSetDataSet->Locate("ID", pharm_group_id, TLocateOptions());
			PharmGroupsSetDataSource->Enabled = true;
			return true;
		}
		PharmGroupsSetDataSet->Next();
	}
	PharmGroupsSetDataSet->Locate("ID", pharm_group_id, TLocateOptions());
	PharmGroupsSetDataSource->Enabled = true;
	return false;
}
//---------------------------------------------------------------------------

void __fastcall TItemDrugGenericNamesForm::SavePharmGroupsSet()
{
	PharmGroupsSetDataSource->Enabled = false;
	int pharm_group_id = PharmGroupsSetDataSet->FBN("ID")->AsInteger;

	ClearPharmGroupSetQuery->ParamByName("GENERIC_NAME_ID")->AsInteger = DataSet->FBN("ID")->AsInteger;
	ClearPharmGroupSetQuery->ExecQuery();
	AddPharmGroupQuery->ParamByName("GENERIC_NAME_ID")->AsInteger = DataSet->FBN("ID")->AsInteger;
	PharmGroupsSetDataSet->First();
	while(!PharmGroupsSetDataSet->Eof) {
		if(PharmGroupsSetDataSet->FBN("IS_SELECTED")->AsInteger) {
			AddPharmGroupQuery->ParamByName("PHARM_GROUP_ID")->AsInteger = PharmGroupsSetDataSet->FBN("ID")->AsInteger;
			AddPharmGroupQuery->ExecQuery();
		}
		PharmGroupsSetDataSet->Next();
	}

	PharmGroupsSetDataSet->Locate("ID", pharm_group_id, TLocateOptions());
	PharmGroupsSetDataSet->FullRefresh();
	PharmGroupsSetDataSource->Enabled = true;
}
//---------------------------------------------------------------------------

void __fastcall TItemDrugGenericNamesForm::CancelPharmGroupsSet()
{
	PharmGroupsSetDataSet->CancelUpdates();
	PharmGroupsSetDataSet->First();
}
//---------------------------------------------------------------------------

void __fastcall TItemDrugGenericNamesForm::PharmGroupsSetDBGridEhDblClick(TObject *Sender)
{
	PharmGroupsSetDataSet->Edit();
	PharmGroupsSetDataSet->FBN("IS_SELECTED")->AsInteger = !PharmGroupsSetDataSet->FBN("IS_SELECTED")->AsInteger;
	PharmGroupsSetDataSet->Post();
	DataSet->Edit();
	DataSet->FieldByName("NAME")->AsString = DataSet->FieldByName("NAME")->AsString;
}
//---------------------------------------------------------------------------

void __fastcall TItemDrugGenericNamesForm::PharmGroupsSetDBGridEhCellClick(TColumnEh *Column)
{
	if(Column->FieldName == "IS_SELECTED" || GetKeyState(VK_CONTROL)&0x8000)
		PharmGroupsSetDBGridEhDblClick(PharmGroupsSetDBGridEh);
}
//---------------------------------------------------------------------------

void __fastcall TItemDrugGenericNamesForm::PharmGroupsSetDBGridEhKeyDown(TObject *Sender, WORD &Key, TShiftState Shift)
{
	if(Key == VK_LEFT)
		DBGridEh->SetFocus();
	else if(Key == ' ')
		PharmGroupsSetDBGridEhDblClick(PharmGroupsSetDBGridEh);
}
//---------------------------------------------------------------------------

void __fastcall TItemDrugGenericNamesForm::DBGridEhKeyDown(TObject *Sender, WORD &Key, TShiftState Shift)
{
	if(Key == VK_RIGHT) PharmGroupsSetDBGridEh->SetFocus();
}
//---------------------------------------------------------------------------

void __fastcall TItemDrugGenericNamesForm::PharmGroupsSetDBGridEhExit(TObject *Sender)
{
	int Fields[2] = {0, 1};
	bool Orders[2] = {false, true};
	PharmGroupsSetDataSet->DoSortEx(Fields, 1, Orders, 1);
}
//---------------------------------------------------------------------------

void __fastcall TItemDrugGenericNamesForm::PharmGroupsSetDBGridEhColumnsGetCellParams(TObject *Sender, bool EditMode, TColCellParamsEh *Params)
{
	if(PharmGroupsSetDataSet->FBN("IS_SELECTED")->AsInteger == 1) {
		Params->Background = TColor(0x00FFE0D0);
		Params->Font->Style = TFontStyles()<<fsBold;
	}
	else {
		Params->Background = TColor(0x00E0FFFF);
		Params->Font->Style = TFontStyles();
	}
}
//---------------------------------------------------------------------------

void __fastcall TItemDrugGenericNamesForm::DataSetAfterScroll(TDataSet *DataSet)
{
	PharmGroupsSetDataSet->ParamByName("MAS_GENERIC_NAME_ID")->AsInteger = DataSet->FieldByName("ID")->AsInteger;
	PharmGroupsSetDataSet->FullRefresh();
	PharmGroupsSetDataSet->First();
}
//---------------------------------------------------------------------------

void __fastcall TItemDrugGenericNamesForm::DataSetBeforePost(TDataSet *DataSet)
{
	if(!DataSet->FieldByName("Name")->AsString.IsEmpty() && PharmGroupsSetDBGridEh->SumList->SumCollection->Items[0]->SumValue == 0) {
		PharmGroupsSetDBGridEh->SetFocus();
		throw Exception("Pharmaceutical groups list can't be empty");
	}
}
//---------------------------------------------------------------------------

void __fastcall TItemDrugGenericNamesForm::DataSetAfterPost(TDataSet *DataSet)
{
	if(IsPharmGroupsSetChanged()) SavePharmGroupsSet();
}
//---------------------------------------------------------------------------

void __fastcall TItemDrugGenericNamesForm::DataSetAfterCancel(TDataSet *DataSet)
{
	CancelPharmGroupsSet();
}
//---------------------------------------------------------------------------

